var common = require('../../common.js');
var app = getApp();
Page({
  data: {
    detail:{},
    userinfo: null,
    wxcode: ''
  },
  getWxCode: function () {
    wx.login({
      success: res => {
        this.setData({
          wxcode: res.code
        });
      }
    });
  },
  onLoad: function (option) {
    this.setData({userinfo:wx.getStorageSync('userInfo')});
    wx.request({
      url: "https://kk.timemost.com/api/index/item",
      method: 'POST',
      data: {
        token:common.token,
        item_id: option.item_id,
        appid:wx.getAccountInfoSync().miniProgram.appId
      },
      success: res => {
        wx.stopPullDownRefresh();
        if (res.data.status == 1) {
          this.data.detail = res.data.data;
          this.setData({detail:this.data.detail});
        }
      }
    });
  },
  onShareAppMessage: function () {

  },
  toBack:function(){
    wx.navigateBack({
      delta: 1
    });
  },
  orderNow:function(){
    wx.getSetting({
      success:res => {
        if (res.authSetting['scope.address']) {
          wx.chooseAddress({
            success:res => {
              this.orderInit(res);
            }
          }); 
        } else {
          if (res.authSetting['scope.address'] == false) {
            wx.openSetting({
              success:res => {
              }
            });
          } else {
            wx.chooseAddress({
              success:res => {
                this.orderInit(res);
              }
            });
          }
        }
      }
    })
  },
  orderInit:function(option){
    if(option.errMsg != 'chooseAddress:ok'){
      wx.showToast({
        title: '地址选择失败',
        icon: 'none'
      });
      return;
    }
    wx.showLoading({
      title: '提交中',
    });
    wx.request({
      url: "https://kk.timemost.com/api/index/iorder",
      method: 'POST',
      data: {
        token:common.token,
        item_id: this.data.detail.item_id,
        name: option.userName,
        mobile: option.telNumber,
        province: option.provinceName,
        city: option.cityName,
        address: option.detailInfo,
        appid:wx.getAccountInfoSync().miniProgram.appId,
        openid: this.data.userinfo.openid
      },
      success: res => {
        wx.hideLoading();
        if (res.data.status == 1) {
          wx.showToast({
            title: '下单成功',
          });
        }else{
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          });
        }
      }
    });
  },
  loginForOrder:function(option){
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          this.getWxCode();
          wx.getUserInfo({
            lang:'zh_CN',
            success: res => {
              wx.request({
                url: "https://kk.timemost.com/api/index/auth",
                method: 'POST',
                data: {
                  token:common.token,
                  iv: res.iv,
                  data: res.encryptedData,
                  code: this.data.wxcode,
                  appid:wx.getAccountInfoSync().miniProgram.appId,
                  raw: res.rawData
                },
                success: ret => {
                  if(ret.data.status == 1){
                    this.setData({userinfo:ret.data.data});
                    wx.setStorageSync('userInfo', ret.data.data);
                    this.orderNow();
                  }
                }
              });
            }
          })
        }
      }
    });
  }
})